#include<unistd.h>
#include<fcntl.h>
#include<cstring>
#include<iostream>
using namespace std;
void setNonblock(int fd)
{
  int fl=fcntl(fd,F_GETFL);
  if(fl<0)
  {
    cerr<<"fcntl error:"<<strerror(errno)<<endl;
    exit(-1);
  }
  fcntl(fd,F_SETFL,fl|O_NONBLOCK);
}
int main()
{
  setNonblock(0);
  char buffer[1024];
  while(1)
  {
    ssize_t s=read(0,buffer,sizeof buffer-1);
    if(s<0)
    {
      if(errno==EAGAIN||errno==EWOULDBLOCK)  //底层数据没有就绪
      {
        cout<<"底层数据没有就绪"<<strerror(errno)<<endl;
        sleep(1);
        continue;
      }
      else if(errno==EINTR)  // 在读取数据之前被信号中断
      {
        cout<<"EINTR :"<<strerror(errno)<<endl;
        sleep(1);
        continue;
      }
      cerr<<"read error:"<<strerror(errno)<<endl;
      break;
    }
    buffer[s]=0;
    cout<<"echo#"<<buffer<<endl;
  }
  return 0;
}
